#include "slist.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


static node_t *__node_create(slist_t *s, void *data)
{   
    node_t *tmp = NULL;

    // 参数检查
    if (NULL == s || NULL == data)
    {
        perror("argument lawless...");
        goto err0;
    }

    // 创建节点空间
    tmp = malloc(sizeof(node_t));
    if (NULL == tmp)
    {
        perror("tmp malloc failed...");
        goto err0;
    }
    memset(tmp, 0, sizeof(node_t));

    // 创建类型空间
    tmp->data = malloc(s->size);
    if (NULL == tmp->data)
    {
        perror("tmp->data malloc failed...");
        goto err1;
    }
    memset(tmp->data, 0, s->size);

    // 写入数据
    memcpy(tmp->data, data, s->size);
    tmp->next = NULL;
    s->count++;

    return tmp;

err1:
    free(tmp);
err0:
    return NULL;
}


int slist_create(slist_t **s, int size, ft1 my_destroy)
{

    // 参数检查
    if (NULL == my_destroy || size <= 0)
    {
        perror("argument lawless...");
        goto err0;
    }

    // 创建头信息结构体空间
    *s = malloc(sizeof(slist_t));
    if (NULL == *s)
    {
        perror("*s malloc failed...");
        goto err0;
    }
    memset(*s, 0, sizeof(slist_t));

    // 初始化
    (*s)->count = 0;
    (*s)->my_destroy = my_destroy;
    (*s)->p = NULL;
    (*s)->size = size;


    return 0;
err0:
    return -1;
}

int slist_append(slist_t *s, void *data)
{
    node_t *new_node = NULL;
    node_t *tmp = NULL;

    // 参数检查
    if (NULL == s || NULL == data)
    {
        perror("argument lawless...");
        goto err0;
    }

    // 节点创建
    new_node = __node_create(s, data);

    // 尾部插入
    tmp = s->p;
    if (NULL == tmp)
    {
        s->p = new_node;
    }
    else 
    {
        while (NULL != tmp->next)
        {
            tmp = tmp->next;
        }
        tmp->next = new_node;
    }

    return 0;
err0:
    return -1;
}


int slist_prepend(slist_t *s, void *data)
{
    node_t *new_node = NULL;

    // 参数检查
    if (NULL == s || NULL == data)
    {
        perror("argument lawless...");
        goto err0;
    }

    // 节点创建
    new_node = __node_create(s, data);

    // 头部插入
    new_node->next = s->p;
    s->p = new_node;

    return 0;
err0:
    return -1;
}

int slist_traverse(slist_t *s, ft1 my_printf)
{   
    node_t *node = NULL;


    // 参数检查
    if (NULL == s || NULL == my_printf)
    {
        perror("argument lawless...");
        goto err0;
    }


    // 遍历
    node = s->p;
    while (NULL != node)
    {
        my_printf(node);
        node = node->next;
    }
    printf("\n");


    return 0;
err0:
    return -1;
}

int slist_destory(slist_t *s)
{
    node_t *tmp = NULL;
    node_t *save = NULL;

    // 参数检查
    if (NULL == s)
    {
        perror("argument lawless...");
        goto err0;
    }

    tmp = s->p;
    s->p = NULL;
    // 销毁
    while (NULL != tmp)
    {
        // 保存
        save = tmp->next;

        s->my_destroy(tmp);
        s->count--;

        // 跳转
        tmp = save;
    }
    s->p = NULL;

    return 0;
err0:
    return -1;
}   


int slist_insert_by_index(slist_t *s, void *num, int index)
{   
    node_t *new_node = NULL;
    node_t *tmp = NULL;
    int idx = index - 1;
    int i = 0;


    // 参数检查
    if (NULL == s || NULL == num)
    {
        perror("argument lawless...");
        goto err0;
    }

    // 检索检查
    if (idx < 0)
    {
        goto err0;
    }
    else if (idx == 0)
    {
        slist_prepend(s, num);
    }   
    else if (idx >= s->count)
    {
        slist_append(s, num);
    }
    else 
    {
        tmp = s->p;
        // 创建节点
        new_node = __node_create(s, num);

        for (i = 0; i < idx - 1; i++)
        {
            tmp = tmp->next;
        }

        new_node->next = tmp->next;
        tmp->next = new_node;
    }



    return 0;
err0:
    return -1;
}


int slist_delete_by_index(slist_t *s, int index, ft1 my_destroy)
{
    node_t *tmp = NULL;
    node_t *save = NULL;
    int idx = index - 1;
    int i = 0;


    // 参数检查
    if (NULL == s)
    {
        perror("argument lawless...");
        goto err0;
    }

    if (s->p == 0 || index < 1 || index > s->count)
    {
        goto err0;
    }
    else if (s->count == 1)
    {
        my_destroy(s->p);
        s->p = NULL;
        s->count--;
    }
    else if (idx == 0)
    {
        save = s->p->next;
        my_destroy(s->p);
        s->count--;
        s->p = save;
    }
    else 
    {
        tmp = s->p;

        for (i = 0; i < idx - 1; i++)
        {
            tmp = tmp->next;
        }
        save = tmp->next->next;
        my_destroy(tmp->next);
        s->count--;
        tmp->next = save;
    }



    return 0;
err0:
    return -1;
}